﻿#include "ObjectManager.h"

//ACT//Codec_NBIoT
#include "Codec_NBIoT.h"
//ACT
#include "UDP_Test.h"
//IO
#include "SerialPortManager.h"

//config file
#include "MacroDefinition.h"

#include "qdebug.h"
ObjectManager* ObjectManager::objectManager=Q_NULLPTR;

ObjectManager::ObjectManager(QObject *parent) : QObject(parent)
{
  objectManager=this;
  objectManager->setObjectName("objectManager");
  objectCreation();
  objectInjection();
  objectInitialization();
  logSaver->saveLog("ObjectManager created");
  if(getObjFromMap<SerialPortManager>()->openSerialPort())
    {
      logSaver->saveLog("open serial port succeed");
      getObjFromMap<Codec_NBIoT>()->AT_CEREG();
      getObjFromMap<UDP_Test>()->startTest();
    }
  else
    {
      logSaver->saveLog("open serial port failed");
    }
}

ObjectManager::~ObjectManager()
{
  for(QString objName : objMap.keys())
    {
      removeFromObjMap(objName);
    }
}

ObjectManager* ObjectManager::getObjectManager()
{
  return Q_NULLPTR==objectManager?objectManager=new ObjectManager():objectManager;
}

void ObjectManager::objectCreation()
{
  logSaver->saveLog("-------------------------objectCreation-------------------------");
  //ACT//Codec_NBIoT
  addToObjMap<Codec_NBIoT>();
  //ACT
  addToObjMap<UDP_Test>();
  //IO
  addToObjMap<SerialPortManager>();
  logSaver->saveLog("-------------------------objectCreation-------------------------");
}

#ifdef OBJECTINJECTION_ON
void ObjectManager::objectInjection()
{
  logSaver->saveLog("-------------------------objectInjection-------------------------");
  for(QObject* obj:objMap.values())
    {
      ObjAutoInject* temp=dynamic_cast<ObjAutoInject*>(obj);
      if(temp)
        {
          temp->objectAutoInjection(&objMap);
        }
    }
  logSaver->saveLog("-------------------------objectInjection-------------------------");
}
#else
void ObjectManager::objectInjection()
{}
#endif

void ObjectManager::objectInitialization()
{
  logSaver->saveLog("-------------------------objectInitialization-------------------------");
  SerialPortManager* serialPortManager=getObjFromMap<SerialPortManager>();
  serialPortManager->setPortName("COM5");
  serialPortManager->setBaudRate(460800);
  serialPortManager->setDataBits(QSerialPort::Data8);
  serialPortManager->setStopBits(QSerialPort::OneStop);
  serialPortManager->setParity(QSerialPort::NoParity);
  serialPortManager->setFlowControl(QSerialPort::NoFlowControl);
  logSaver->saveLog("serial port name : COM5");
  logSaver->saveLog("serial port baudRate : 460800");
  logSaver->saveLog("serial port dataBits : Data8");
  logSaver->saveLog("serial port stopBits : OneStop");
  logSaver->saveLog("serial port parity : NoParity");
  logSaver->saveLog("serial port flowControl : NoFlowControl");
  logSaver->saveLog("-------------------------objectInitialization-------------------------");
}

void ObjectManager::removeFromObjMap(QObject* obj)
{
  if(Q_NULLPTR==obj)
    {
      return;
    }
  removeFromObjMap(obj->objectName());
}

void ObjectManager::removeFromObjMap(QString objName)
{
  QString name=objName;
  if(name.isEmpty())
    {
      logSaver->saveLog(QString("removeFromObjMap , obj name is empty"));
      return;
    }
  if(!objMap.contains(name))
    {
      logSaver->saveLog(QString("removeFromObjMap , obj %1 is not in objMap").arg(name));
      return;
    }
  objMap.take(name)->deleteLater();
  logSaver->saveLog(QString("removeFromObjMap , obj %1 is removed from objMap").arg(name));
}

void ObjectManager::addToObjMap(QObject* obj,QString objName)
{
  if(Q_NULLPTR==obj)
    {
      logSaver->saveLog("addToObjMap , obj is null");
      return;
    }
  obj->setObjectName(objName);
  QString name=obj->objectName();
  if(name.isEmpty())
    {
      logSaver->saveLog(QString("addToObjMap , obj name is empty"));
      obj->deleteLater();
      return;
    }
  if(objMap.contains(name))
    {
      logSaver->saveLog(QString("addToObjMap , obj %1 is already in objMap").arg(name));

      obj->deleteLater();
      return;
    }
  objMap.insert(name,obj);
  logSaver->saveLog(QString("addToObjMap , obj %1 is added to objMap").arg(name));
}







